<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <style>
        body{
            text-align: center;
        }
        canvas {
            border: 1px solid #000;

        }
    </style>
</head>
<body>
    <canvas width="500" height="500" id="cas"></canvas>
</body>
<script>
    var cas=document.getElementById('cas');
    var ctx=cas.getContext('2d');
   var x=100,
       y=100,
       w=100,
       h=100,
       step=3;
    var intervalId=setInterval(function(){
        ctx.clearRect(x,y,w,h);
//        cas.width=cas.width;
        x+=step;
        if(x>=400){
            x=400;
            clearInterval(intervalId);
        }
        ctx.fillRect(x,y,w,h);
    },20)
</script>
</html>